package com.vancouver.knows.business.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.pangu.bean.PageInfo;
import com.pangu.service.ActionException;

/**
 * json 解析父类<br>
 * 如果检查到状态码有异常的，直接抛出无需处理剩余的部分，由服务接口捕获处理。
 * <p/>
 * Created by cxlin
 */
public abstract class ACheckableJsonParser {
	// 页面信息
	protected PageInfo pageInfo;

	protected String checksum;

	protected boolean isChecksumMatched = false;

	/**
	 * 解析json
	 * 
	 * @param data
	 * @throws ActionException
	 */
	public void parserJson(byte[] data) throws ActionException {
		parserJson(data, "utf-8");
	}

	/**
	 * 解析json
	 * 
	 * @param data
	 * @param charset
	 * @throws ActionException
	 */
	public void parserJson(byte[] data, String charset) throws ActionException {
		try {
			String dataStr = new String(data, charset);
			JSONObject result = JSON.parseObject(dataStr);
			// 这里使用get获取值，因为我们希望捕获异常
			int code = result.getInteger("error_code");
			if (isSuccess(code)) {
				parserPageinfo(result);
				parserSearchPageinfo(result);
				parserContent(result);
				checksum = result.getString("checksum");
				isChecksumMatched = false;

			} else if (isChecksumMatched(code)) {
				parserPageinfo(result);
				parserSearchPageinfo(result);
				isChecksumMatched = true;

			} else {
				String exMsg = result.getString("error_message");
				ActionException actionException = new ActionException();
				actionException.setExCode(code);
				actionException.setExMessage(exMsg);

				throw actionException;
			}

		} catch (Exception e) {
			e.printStackTrace();
			if (e instanceof ActionException) {
				throw (ActionException) e;

			} else {
				throw new ActionException(ActionException.OPERATE_ERROR, "数据解析异常");
			}
		}
	}

	protected boolean isSuccess(int errorCode) {
		return errorCode == 0;
	}

	protected boolean isChecksumMatched(int errorCode) {
		return errorCode == 200;
	}

	/**
	 * 获得页面数量相关的统计信息
	 * 
	 * @param jsonPageInfo
	 * @throws Exception
	 */
	protected void parserPageinfo(JSONObject jsonPageInfo) {
		try {
			int totalCount = jsonPageInfo.getInteger("total_count");
			if (totalCount != 0) {
				pageInfo = new PageInfo();
				pageInfo.setFrom(jsonPageInfo.getInteger("from"));
				pageInfo.setSize(jsonPageInfo.getInteger("size"));
				pageInfo.setTotalCount(totalCount);
			}
		} catch (Exception e) {

		}
	}

	/**
	 * 获得页面数量相关的统计信息
	 * 
	 * @param jsonPageInfo
	 * @throws Exception
	 */
	protected void parserSearchPageinfo(JSONObject jsonPageInfo) {
		try {
			JSONObject jsonPostRult = jsonPageInfo.getJSONObject("post_result");
			JSONObject jsonEventRult = jsonPageInfo.getJSONObject("event_result");
			int totalPostCount = jsonPostRult.getInteger("total_count");
			if (totalPostCount != 0) {
				pageInfo = new PageInfo();
				pageInfo.setFrom(jsonPostRult.getInteger("from"));
				pageInfo.setSize(jsonPostRult.getInteger("size"));
				pageInfo.setTotalCount(totalPostCount);
			}

			int totalEventCount = jsonEventRult.getInteger("total_count");
			if (totalEventCount != 0) {
				pageInfo = new PageInfo();
				pageInfo.setFrom(jsonEventRult.getInteger("from"));
				pageInfo.setSize(jsonEventRult.getInteger("size"));
				pageInfo.setTotalCount(totalEventCount);
			}
		} catch (Exception e) {

		}
	}

	public PageInfo getPageInfo() {
		return pageInfo;
	}

	protected abstract void parserContent(JSONObject content) throws Exception;

}
